package com.example.demo.service.impl;

import com.example.demo.mapper.BackLoginMapper;
import com.example.demo.pojo.Admin;
import com.example.demo.pojo.Backlogin;
import com.example.demo.service.BackLoginService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class BackLoginImpl implements BackLoginService {
    int stateid;
    @Resource
    BackLoginMapper backLoginMapper;
    @Resource
    BackLoginService backLoginService;
    @Override
    public List<Admin> Backselect(int backseq, String backname, String pwd) {
        List<Admin> backselect =backLoginMapper.Backselect(backseq,backname,pwd);
        return backselect;
    }

    @Override
    public PageInfo<Admin> backlogininfo(Integer page, Integer size, int backseq, String backname, String pwd) {
        PageHelper.startPage(page,size);
        List<Admin> backlogins =backLoginMapper.Backselect(backseq,backname,pwd);
        PageInfo<Admin> backloginPageInfo =new PageInfo<>(backlogins);
        return backloginPageInfo;
    }

    @Override
    public String backupdate(String name, String account, String state, String pwd, String seq,String page,int backroleid) {
        String message="";
        int state_id;
        if(state.equals("")){
            state_id=-1;
        }else {
            state_id=Integer.parseInt(state);
            System.out.println(state+""+seq+""+page);
        }
        if(!account.equals("") && !name.equals("")){
            List<Admin> backselect =backLoginMapper.Backselect(-1,name,"");
            List<Admin> backselect1 =backLoginMapper.Backselect(-1,account,"");
            if(backselect.size()>1 && backselect1.size()>1){

                message="账号已经存在";
            }else{
                backLoginMapper.backupdate(name,account,state_id,pwd,Integer.parseInt(seq),backroleid);
                message="添加成功";
            }
        }else{
            backLoginMapper.backupdate(name,account,state_id,pwd,Integer.parseInt(seq),backroleid);
            message="修改成功";
        }
        return message;
    }

    @Override
    public void backdelete(String seq) {
        backLoginMapper.backdelete(Integer.parseInt(seq));
    }

    @Override
    public String backinsert(String name, String pwd, String count, String role) {
        String message="";
        List<Admin> backselect =backLoginMapper.Backselect(-1,name,"");
        List<Admin> backselect1 =backLoginMapper.Backselect(-1,count,"");
        if(backselect.size()>0){
            message="用户名已经存在";
        }else if(backselect1.size()>0){
            message="用户账号已经存在";
        }else{
            backLoginMapper.backinsert(name,pwd,count,Integer.parseInt(role));
            message="添加成功";
        }
        return message;
    }

    @Override
    public String loginuril(String count, String password, String yzm, HttpSession httpSession) {
        String code=(String) httpSession.getAttribute("code");
        List<Admin> adminList = backLoginMapper.login(count,password);

        if(!yzm.equals(code)){
            return "验证码错误";
        }else if(count.equals("")){
            return "用户不为空";
        }else if (password.equals("")){
            return "密码不能为空";
        }else if(adminList.size()==0){
            return "用户不存在或密码错误";
        }else if(adminList.get(0).getStateName().equals("禁用")){
            return "账号已被禁用";
        }else if(adminList.size()>0){
            httpSession.setAttribute("backuserid",adminList.get(0).getBackSeq());
            httpSession.setAttribute("backname",adminList.get(0).getBackName());
            return "true";
        }
        return "用户不存在或密码错误";
    }

}
